package com.zyd.controller.blackcar;

import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.model.BlackCarEntity;
import com.zyd.io.page.PageBean;
import com.zyd.io.vo.page.MultiQueryPageVO;
import com.zyd.io.vo.res.ResBody;
import com.zyd.service.blackcar.BlackCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blackcars")
public class BlackCarController {

    @Autowired
    private BlackCarService service;

    @PostMapping(value = "/addBlackcar", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody addBlackCar(@RequestBody BlackCarEntity blackcar) {
        try {
            service.addBlackcar(blackcar);
            return new ResBody(ErrorDef.SUCCESS, "新增黑车成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "新增黑车失败");
        }
    }

    @PostMapping(value = "/updateBlackcar", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody updateBlackCar(@RequestBody BlackCarEntity blackcar) {
        try {
            service.updateBlackcar(blackcar);
            return new ResBody(ErrorDef.SUCCESS, "修改黑车成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "修改黑车失败");
        }
    }

    @PostMapping(value = "/deleteBlackcar", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody deleteBlackCar(@RequestBody Map<String, Object> map) {
        try {
            service.deleteBlackcar((String) map.get("id"));
            return new ResBody(ErrorDef.SUCCESS, "删除黑车成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "删除黑车失败");
        }
    }

    @PostMapping(value = "/blackcarList", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findUseList(@RequestBody MultiQueryPageVO blackcarRequestDTO) {
        try {
            PageBean<Map<String, Object>> page = service.findBlackcarPage(blackcarRequestDTO);

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询黑车列表成功");
            resBody.setData(page);
            return resBody;
        } catch (BusinessException e) {
            e.printStackTrace();
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查看黑车列表失败");
        }
    }

    @PostMapping(value = "/findBlackcar", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findBlackCar(@RequestBody Map<String, Object> map) {
        try {
            BlackCarEntity blackcar = service.findBlackcar((String) map.get("id"));
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询黑车成功");
            resBody.setData(blackcar);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询黑车失败");
        }
    }


    @PostMapping(value = "/isWarn", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody isWarn() {
        try {
            Map<String, Object> ret = service.blackWarn();
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询黑车预警成功");
            resBody.setData(ret);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询黑车预警失败");
        }
    }

    @PostMapping(value = "/sendCarImg", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody sendCarImg(@RequestBody Map<String, Object> map) {
        try {
            System.out.println("tag_id" + (String) map.get("tagid"));
            Map resultMap = service.findBlackcarId((String) map.get("tagid"));
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询黑车图片成功");
            resBody.setData(resultMap);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询黑车图片失败");
        }
    }

    @PostMapping(value = "/driveRecord", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody driveRecord(@RequestBody Map<String, Object> map) {
        try {
            ArrayList<Object> list = new ArrayList<>();
            ArrayList tag_ids = (ArrayList) map.get("tag_ids");
            System.out.println(tag_ids);
            for (int i = 0; i < tag_ids.size(); i++) {

                Map result = service.findByCommonCode((String) tag_ids.get(i));
                list.add(result);
            }
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询黑车最新行车记录成功");
            resBody.setData(list);
            return resBody;
//            resBody.setData(arr);
//            System.out.println(resBody);
//            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "没有当前黑车最新行车记录");
        }

    }
//    黑车统计 查询

    @PostMapping(value = "/findBlackList", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody blackCarFindList(@RequestBody MultiQueryPageVO blackcarRequestDTO) {
        try {
            PageBean<Map<String, Object>> blackPage = service.blackCarFindPage(blackcarRequestDTO);

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询找回黑车列表成功");
            resBody.setData(blackPage);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查看找回黑车列表失败");
        }
    }

    /**
     * 获取黑车滚动预警 前台每五分钟查一次 在黑车轨迹表获得五分之前到当前时间的数据
     *
     * @param
     * @return
     */
    @PostMapping(value = "/getBlackInfo", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody getBlackInfo() {
        try {

            List resultList = service.getBlackInfo();
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询黑车警告滚动成功");
            resBody.setData(resultList);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询黑车警告滚动失败");
        }
    }

    /**
     * 通过area查询 该给谁发送短信
     *
     * @param param {area:'1'}
     * @return
     */
    @PostMapping(value = "/findBlackSmsInfo", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findBlackSmsInfo(@RequestBody Map<String, String> param) {
        try {
            String area = param.get("area");
            if (StringUtils.isEmpty(area)) {
                return new ResBody(ErrorDef.SUCCESS, "查询黑车短信列表失败,参数不正确");
            }
            List resultList = service.findBlackSmsInfo(area);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询黑车短信列表");
            resBody.setData(resultList);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询黑车短信列表失败");
        }
    }

    /**
     * 保存
     *
     * @param param {[id:1],area:'1',name:'张三',mobile:'18845688585',area:'1'}
     * @return
     */
    @PostMapping(value = "/saveBlackSmsInfo", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody saveBlackSmsInfo(@RequestBody Map<String, String> param) {
        try {
            service.saveBlackSmsInfo(param);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "保存成功");
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "保存失败");
        }
    }


    /**
     * 删除
     *
     * @param param {id:1}
     * @return
     */
    @PostMapping(value = "/deleteBlackSmsInfo", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody deleteBlackSmsInfo(@RequestBody Map<String, String> param) {
        try {
            String id = param.get("id");
            if (StringUtils.isEmpty(id)) {
                return new ResBody(ErrorDef.SUCCESS, "操作失败,参数不正确");
            }
            service.deleteBlackSmsInfo(id);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "删除成功");
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "操作失败");
        }
    }


}
